﻿/// <reference path="../ext-all.js" />
/// <reference path="../../MyJS/MyAjax.js" />

//自动选用Ext或者JQ进行通信
var ExtAjax = {
    callServer: function (sUrl, sData, bAsync, sType, oCallBackFunction, vCallBackFunctionParameters) {
        /// <summary>与服务器通信</summary>
        /// <param name="sUrl" type="String">服务器处理请求的url路径</param>
        /// <param name="sData" type="String">post到服务器端的数据</param>
        /// <param name="bAsync" type="Boolean">是否异步请求；true：异步；false：同步（默认值）</param>
        /// <param name="sType" type="String">只能取两种值：get/post</param>
        /// <param name="oCallBackFunction" type="Function">回调的方法</param>
        /// <param name="vCallBackFunctionParameters" type="Object">传递给回调方法的参数</param>
        if (typeof (bAsync) == "undefined" || bAsync == null) bAsync = false; //默认采用同步的模式通信
        var sResult = "";
        Ext.Ajax.request({
            url: sUrl,
            method: sType,
            params: sData,
            async: bAsync, //ASYNC 是否异步( TRUE 异步 , FALSE 同步)
            success: function (response, options) {
                sResult = response.responseText;
                if (oCallBackFunction) {
                    oCallBackFunction(response.responseText, aCallBackFunctionParameters);
                }
            },
            failure: function (response, options) {
            }
        });
        return sResult;
    }
};

//实现 MyAjax 接口
(function () {
    if (typeof (MyAjax) != "undefined") {
        MyAjax.post = function (sUrl, sPostData, bAsync, oCallBackFunction, aCallBackFunctionParameters) {
            return ExtAjax.callServer(sUrl, sPostData, bAsync, "POST", oCallBackFunction, aCallBackFunctionParameters);
        };

        MyAjax.get = function (sUrl, vParam, bAsync, oCallBackFunction, aCallBackFunctionParameters) {
            return ExtAjax.callServer(sUrl, vParam, bAsync, "GET", oCallBackFunction, aCallBackFunctionParameters);
        };
    }
})();